import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import _get_table from '../atom/get-table'
import _get_tables from '../atom/get-tables'
import login from '../atom/login'
import _parse_api from '../atom/parse-api'
import _test_model from '../atom/test-model'
import _tree_data from '../atom/tree-data'
import { IObj } from '../interface'
import { Projects } from './entity';
import { IProject, ITestParams } from './interface'

@Injectable()
export class ProjectsService {
	constructor(
		@InjectRepository(Projects)
		private readonly projectssRepository: Repository<Projects>
	) { }

	/**
	 * 新增项目
	 * @param body
	 */
	async add(body: IProject) {
		await login(body.nodejs, body.user, body.password)
		return this.projectssRepository.save(body).then((res) => {
			return {
				code: 1,
				msg: 'success',
				project_id: res.project_id
			}
		})
	}

	/**
	 * 文档详情
	 * @param project_id string
	 */
	async find(project_id: string) {
		const res = await this.projectssRepository.findOne({
			project_id
		}) as IProject;
		if (res) {
			return {
				data: res,
				code: 1,
				msg: 'success'
			}
		} else {
			return {
				data: {},
				code: 0,
				msg: 'no data'
			}
		}
	}

	/**
	 * 修改文档
	 * @param body IProject
	 */
	async edit(body: IProject) {
		const res = await this.find(body.project_id);
		if (res.code === 1) {
			const obj = { ...res.data, ...body }
			await this.projectssRepository.save(obj)
			return {
				code: 1,
				msg: 'success'
			}
		}
		else {
			return res
		}
	}

	list() {
		return this.projectssRepository.find()
	}

	del(project_id) {
		return this.projectssRepository.delete({
			project_id
		})
	}

	/**
	 * 获取数据库内的所有表
	 * @param project_id
	 */
	async get_tables(project_id: string) {
		const res = await this.find(project_id)
		if (res.code === 1) {
			const data = res.data as IProject
			const nodejs = data.nodejs;
			const user = await login(data.nodejs, data.user, data.password)
			const tables = await _get_tables(nodejs, user.sessionID)
			return {
				sessionid: user.sessionID,
				tables
			}
		} else {
			return {
				tables: [],
				sessionid: ''
			}
		}
	}

	/**
	 * 测试服务接口
	 * @param body
	 */
	async testmodel(body: ITestParams) {
		const res = await this.find(body.project_id)
		if (res.code === 1) {
			const data = res.data as IProject
			const nodejs = data.nodejs;
			const ret = await _test_model(nodejs, body.message_type, body.params, body.sessionid)
			const tables = body.tables.length === 0 ? [] : await _get_table(nodejs, body.sessionid, body.tables)
			const response = (() => {
				if (ret.code === 1) {
					return _parse_api('root', 'response', ret, tables)
				} else {
					return [ret]
				}
			})()
			const request = _parse_api('root', 'request', (body.params as unknown as IObj), tables)
			return {
				code: 1,
				request,
				response,
				data: ret
			}
		} else {
			return {
				code: 0,
				request: [],
				response: [],
				data: {}
			}
		}
	}
}
